//
//  GamePadViewController.m
//  GravityControl
//
//  Created by Javier Quevedo on 01/07/2009.
//  Copyright 2009 VRIJE. All rights reserved.
//

#import "GamePadViewController.h"
#import "GravityAgent.h"
#import "ConnectionManager.h"

@implementation GamePadViewController
@synthesize shieldView, lock, isShieldTouchedDown;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

- (IBAction) shieldTouchDown: (id) sender{
	NSLog(@"Touch Down");
	[lock lock];
	isShieldTouchedDown=YES;
	[lock unlock];
}
- (IBAction) shieldTouchUp: (id) sender{
	[lock lock];
	isShieldTouchedDown=NO;
	[lock unlock];
}

- (IBAction) bombTouchUpInside: (id) sender{
	currentBomb +=1.0;
}
-(void) trackNotificationAccelerometer: (NSNotification *)notificacion{
	
	//NSString *msgString = [NSString stringWithFormat:@"ACX %f ACY %f ACZ %f SHIELD %B",gravityAgent.accel.x, gravityAgent.accel.y, gravityAgent.accel.z, isShieldTouchedDown];
	//[connectionManager send:msgString];
	
} 

-(void) onNetTimerTick{
	NSString *msgString = [NSString stringWithFormat:@"ACX %f ACY %f ACZ %f SVAL %f SPRESSED %d BOMB %f",
						   gravityAgent.accel.x, 
						   gravityAgent.accel.y,
						   gravityAgent.accel.z, 
						   shieldStatus, 
						   isShieldTouchedDown, 
						   currentBomb];
	[connectionManager send:msgString];
	
}

- (void) onShieldTimerTick{
	[lock lock];
	if (isShieldTouchedDown==YES){
		if (shieldStatus > 0.0)
			shieldStatus -= 1.0/kShieldTimeIntervals;
		else
			shieldStatus = 0.0;
	}
	else{
		if (shieldStatus < 1.0)
			shieldStatus  += 1.0/kShieldTimeIntervals*kShieldGrowRatio;
		else 
			shieldStatus = 1.0;
		
	}
	[shieldView setValue:shieldStatus animated:YES];
	[lock unlock];
}





- (void) viewWillAppear: (BOOL)animated {
	[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];         
	if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {      
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation((-3.141519/2));
        self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
	}	
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	shieldStatus = 1.0;
	currentBomb = 0.0;
	
	gravityAgent = [GravityAgent sharedAgent];
	connectionManager = [ConnectionManager sharedManager];
	[connectionManager connect];
	
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(trackNotificationAccelerometer:) 
												 name:nGravityUpdated object:gravityAgent];
	
	
	NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:kTotalShieldTime/kShieldTimeIntervals
													  target:self
													selector:@selector(onShieldTimerTick)
													userInfo:NULL
													 repeats:YES];
    [timer fire];
	
	NSTimer *netTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/kNetUpdateFrequency
													  target:self
													selector:@selector(onNetTimerTick)
													userInfo:NULL
													 repeats:YES];
	[netTimer fire];
	
	//[NSThread detachNewThreadSelector:@selector(shieldManagerThread:) toTarget:self withObject:shieldView];
}



// Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return YES;
	// return YES for supported orientations
  //  return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
